home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / SOUND.SWG / 0063_Playing Digital sounds.pas < prev   
Pascal/Delphi Source File  |  1995-02-28  |  3KB  |  143 lines

  1. {
  2. Hi!
  3.  
  4. MA> 4.  I have all my GUS routines working, but can anyone point me to
  5. MA>     a place where I can get ANY *PUBLIC DOMAIN* SB/SBPro code which
  6. MA>     would help me play/mix digital samples?  (I'm not talking
  7. MA>     CT-VOICE VOC code here...).
  8. MA>
  9. MA> As I said, ANY help would be appreciated.
  10.  
  11. Here you are! This piece of code try to show how to play digitized voices
  12. through Sound Blaster. It's from our little SB's Programming Tutorial which
  13. is PD, so be free to distribute it freely!
  14. }
  15. Program SBPlay;
  16.  
  17. {------------------------------------
  18.  SB compatible digital sound routines
  19.  
  20.       Copyright Tapio Äijälä 1994.
  21.  
  22.      Use and distribute freely, but
  23.     please notice me in somewhere on
  24.      your product if you use these
  25.                routines!
  26.  
  27.                 Thanks.
  28.  ------------------------------------}
  29.  
  30. Uses crt;
  31.  
  32. Const DmaChannel                                : Array [0..3,1..3] of Byte =
  33.  
  34. (($87,$0,$1),($83,$2,$3),($81,$2,$3),($82,$6,$7));
  35. Var   Reset, ReadData, WriteData, DataAvailable : Word;
  36.       Offset, Page                              : Word;
  37.  
  38. Function InitSoundSystem(Base : Word) : Byte;
  39.  
  40. {Check out Sound Blaster from given base address}
  41.  
  42. Begin
  43.  InitSoundSystem := 1;
  44.  Base := Base * $10;
  45.  Reset := Base + $206;
  46.  ReadData := Base + $20A;
  47.  WriteData := Base + $20C;
  48.  DataAvailable := Base + $20E;
  49.  Port[Reset] := 1;
  50.  Delay(1);
  51.  Port[Reset] := 0;
  52.  Delay(1);
  53.  If (Port[DataAvailable] And $80 = $80) And (Port[ReadData] = $AA) then Begin
  54.   InitSoundSystem := 0;
  55.  End;
  56. End;
  57.  
  58. Procedure WriteDSP(Data : Byte);
  59.  
  60. {Write a one byte to DSP.}
  61.  
  62. Begin
  63.  While Port[WriteData] And $80 <> 0 Do;
  64.  Port[WriteData] := Data;
  65. End;
  66.  
  67. Function ReadDSP : Byte;
  68.  
  69. {Read a one byte from DSP.}
  70.  
  71. Begin
  72.  While Port[DataAvailable] And $80 = 0 Do;
  73.  ReadDSP := Port[ReadData];
  74. End;
  75.  
  76. Procedure SpeakerOn;
  77.  
  78. {Send sound to line output.}
  79.  
  80. Begin
  81.  WriteDSP($D1);
  82. End;
  83.  
  84. Procedure SpeakerOff;
  85.  
  86. {Don't send anything to line output. Playing will continue, but you don't
  87.  hear anything!}
  88.  
  89. Begin
  90.  WriteDSP($D3);
  91. End;
  92.  
  93. Procedure DMAStop;
  94.  
  95. {Stop DMA-transfer}
  96.  
  97. Begin
  98.  WriteDSP($D0);
  99. End;
  100.  
  101. Procedure DMAContinue;
  102.  
  103. {Continue DMA-transfer}
  104.  
  105. Begin
  106.  WriteDSP($D4);
  107. End;
  108.  
  109. Procedure PlaySample(Sample : Pointer; Size : Word; Freq : Word; DMACh : Byte);
  110.  
  111. {Play data from pointer sample through SB:
  112.  
  113.  Size           Size of data block (Max. 64 Kb in one time!)
  114.  Freq           Sampling rate in herts
  115.  DMACh          Number of DMA-channel (0-3)}
  116.  
  117. Begin
  118.  SpeakerOn;
  119.  Dec(Size);
  120.  Offset := Seg(Sample^) Shl 4 + Ofs(Sample^);
  121.  Page := (Seg(Sample^) + Ofs(Sample^) Shr 4) Shr 12;
  122.  Port[$0A] := $4 + DMACh;
  123.  Port[$0C] := 0; {Clear the internal DMA flip-flop}
  124.  Port[$0B] := $48 + DMACh;
  125.  Port[DMAChannel[1,2]] := Lo(Offset);
  126.  Port[DMAChannel[1,2]] := Hi(Offset);
  127.  Port[DMAChannel[1,1]] := Page;
  128.  Port[DMAChannel[1,3]] := Lo(Size);
  129.  Port[DMAChannel[1,3]] := Hi(Size);
  130.  Port[$0A] := DMACh;
  131.  WriteDSP($40);
  132.  WriteDSP(256 - 1000000 Div Freq);
  133.  WriteDSP($14);
  134.  WriteDSP(Lo(Size));
  135.  WriteDSP(Hi(Size));
  136. End;
  137.  
  138. Begin
  139.  If InitSoundSystem($2) <> 0 then Writeln('Error in initializing soundcard!');
  140. {Check out for SB in give base address!}
  141. PlaySample(Ptr($B800,0),65535,14000,1); {Plays data from video memory through
  142. SB!}End.
  143.